package eiisan.config.mvc.error;

import com.fasterxml.jackson.core.JsonProcessingException;
import eiisan.util.http.ServletUtil;
import eiisan.util.model.response.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.converter.HttpMessageConversionException;
import org.springframework.web.bind.ServletRequestBindingException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.context.request.async.AsyncRequestTimeoutException;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.rmi.ServerException;

/***
 * 错误信息处理
 */
@Slf4j
@ControllerAdvice
public class GlobalController {

	/**
	 * 默认异常处理器
	 * @param serverException 异常
	 * @throws IOException
	 * @throws JsonProcessingException
	 */
	@ExceptionHandler(value = ServerException.class)
	public void serviceExceptionHandler(HttpServletResponse response, ServerException e)
		throws JsonProcessingException, IOException {
		log.error("[eiisan.config.mvc.error.GlobalController.exceptionHandler]", e);
		ServletUtil.writeJson(response, Result.errorServe(e));
	}

	/**
	 * 默认异常处理器
	 * @param HttpServletResponse 响应
	 * @param Exception 异常
	 * @throws IOException
	 * @throws JsonProcessingException
	 */
	@ExceptionHandler(value = Exception.class)
	public void exceptionHandler(HttpServletResponse response, Exception e) throws JsonProcessingException,
            IOException {
		log.error("[eiisan.config.mvc.error.GlobalController.exceptionHandler]{}", e.getMessage());
		ServletUtil.writeJson(response, Result.errorServe(e));
	}

	/**
	 * 默认异常处理器
	 * @param HttpServletResponse 响应
	 * @param HttpMessageConversionException 异常
	 * @throws IOException
	 * @throws JsonProcessingException
	 */
	@ExceptionHandler(value = HttpMessageConversionException.class)
	public void httpMessageConversionExceptionHandler(HttpServletResponse response, HttpMessageConversionException e)
		throws JsonProcessingException, IOException {
		log.error("[eiisan.config.mvc.error.GlobalController.httpMessageConversionExceptionHandler]{}", e.getMessage());
		ServletUtil.writeJson(response, Result.errorRequest());
	}

	/**
	 * 默认异常处理器
	 * @param HttpServletResponse 响应
	 * @param ServletRequestBindingException 异常
	 * @throws IOException
	 * @throws JsonProcessingException
	 */
	@ExceptionHandler(value = ServletRequestBindingException.class)
	public void servletRequestBindingExceptionHandler(HttpServletResponse response, ServletRequestBindingException e)
		throws JsonProcessingException, IOException {
		log.error("[eiisan.config.mvc.error.GlobalController.servletRequestBindingExceptionHandler]{}", e.getMessage());
		ServletUtil.writeJson(response, Result.errorRequest());
	}

	/**
	 * 默认异常处理器
	 * @param HttpServletResponse 响应
	 * @param ServletRequestBindingException 异常
	 * @throws IOException
	 * @throws JsonProcessingException
	 */
	@ExceptionHandler(value = AsyncRequestTimeoutException.class)
	public void asyncRequestTimeoutExceptionHandler(HttpServletResponse response, AsyncRequestTimeoutException e)
		throws JsonProcessingException, IOException {
		log.error("[eiisan.config.mvc.error.GlobalController.asyncRequestTimeoutExceptionHandler]{}", e.getMessage());
		ServletUtil.writeJson(response, Result.busyServe());
	}

}
